package com.zmn.plat.dubbo.impl.expressage;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.business.impl.expressage.KuaiDi100ServiceImpl;
import com.zmn.plat.business.interfaces.expressage.ExpressageService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.expressage.ExpressageCompanyDRO;
import com.zmn.plat.common.dto.expressage.ExpressageDRO;
import com.zmn.plat.dubbo.interfaces.expressage.ExpressageListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.dto.expressage.ExpressageCompanyDTO;
import com.zmn.plat.model.dto.expressage.ExpressageDTO;
import com.zmn.plat.utils.bean.BeanCopyUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 快递服务
 *
 * @author quanjic
 * @version v1.0
 * @since 2018/11/28 14:39
 **/
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
@Deprecated
public class ExpressageListRemoteServiceImpl implements ExpressageListRemoteService {

    @Autowired
    private ExpressageService expressageService;

    @Override
    public ResponseDTO<ExpressageDRO> getByCompanyAndCode(String company, String code) {
        log.info("getByCompanyAndCode 参数为：company={}，code={}", company, code);
        if (StringUtils.isEmpty(company) || StringUtils.isEmpty(code)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        try {
            ExpressageDTO dto = expressageService.find(company, code);
            ExpressageDRO dro = null;
            if (dto != null) {
                dro = new ExpressageDRO();
                BeanCopyUtils.copyProperties(dto, dro);
            }
            return ResponseDTO.success(dro, "OK");
        } catch (Exception e) {
            log.error("#expressage#remote#find error:{}, code:{}", company, code, e);
            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
        }
    }

    @Override
    public ResponseDTO<List<ExpressageCompanyDRO>> listCompanyByCode(String code) {
        log.info("listCompanyByCode 参数为：code={}", code);

        if (StringUtils.isEmpty(code)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        try {
            List<ExpressageCompanyDTO> dto = expressageService.findCompany(code);
            List<ExpressageCompanyDRO> dro = null;
            if (CollectionUtils.isNotEmpty(dto)) {
                dro = new ArrayList<>(dto.size());
                BeanCopier copier = BeanCopier.create(ExpressageCompanyDTO.class, ExpressageCompanyDRO.class, false);
                for (ExpressageCompanyDTO each : dto) {
                    ExpressageCompanyDRO v = new ExpressageCompanyDRO();
                    copier.copy(each, v, null);
                    dro.add(v);
                }
            }
            return ResponseDTO.success(dro, "OK");
        } catch (Exception e) {
            log.error("#expressage#remote#findCompany error:{}, code:{}", code, e);
            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
        }
    }

    @Override
    public ResponseDTO<List<ExpressageCompanyDRO>> listCompany() {
        List<ExpressageCompanyDTO> dto = expressageService.listCompany();
        List<ExpressageCompanyDRO> dro = null;
        if (CollectionUtils.isNotEmpty(dto)) {
            dro = new ArrayList<>(dto.size());
            BeanCopier copier = BeanCopier.create(ExpressageCompanyDTO.class, ExpressageCompanyDRO.class, false);
            for (ExpressageCompanyDTO each : dto) {
                ExpressageCompanyDRO v = new ExpressageCompanyDRO();
                copier.copy(each, v, null);
                dro.add(v);
            }
        }
        return ResponseDTO.success(dro, "OK");
    }

    @Override
    public ResponseDTO getSubscribeByCompanyAndCode(String company, String code) {
        log.info("listCompanyByCode 参数为：code={},company={}", code, company);
        if (StringUtils.isEmpty(code)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        try {
            expressageService.subscribe(company, code);
            String result = null;
            return ResponseDTO.success(result, "OK");
        } catch (Exception e) {
            log.error("#expressage#remote#subscribe error:{}, code:{}", company, code, e);
            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
        }
    }

    @Autowired
    private KuaiDi100ServiceImpl kuaiDi100Service;

    @Override
    public ResponseDTO callbackWithKuaiDi100(String content) {
        log.info("listCompanyByCode 参数为：content={}", content);

        if (StringUtils.isEmpty(content)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        try {
            kuaiDi100Service.callback(content);
            String result = null;
            return ResponseDTO.success(result, "OK");
        } catch (Exception e) {
            log.error("#expressage#remote#callbackWithKuaiDi100 content:{}", content, e);
            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
        }
    }
}
